#include "VButton.h"
#include "VPicture.h"

VButton::VButton(void) :
    VComplexWidget(),
    _pic1(NULL),
    _pic2(NULL),
    _pressed(false),
    _clickevent(NULL),
    _nopopup(false),
    _status(false),
    _havepic(true),
    _privatedata(NULL)
{
    _pic1 = new VPicture;
    _pic2 = new VPicture;

    _pic1->setVisible(true);
    _pic2->setVisible(false);

    init();
}

VButton::~VButton(void)
{
    SAFE_RELEASE(_pic1);
    SAFE_RELEASE(_pic2);
}

bool VButton::loadTexture(const std::string& normal, const std::string& pressed /*= string()*/)
{
    if (!_havepic)
    {
        return false;
    }

    bool ret = true;

    ret = _pic1->loadTexture(normal) && ret;

    if (pressed.empty())
    {
        SAFE_RELEASE(_pic2);

        _pic2 = _pic1;

        ret = true;
    }
    else
    {
        ret = _pic2->loadTexture(pressed) && ret;
    }

    return ret;
}

bool VButton::mouseDownEvent(int x, int y)
{
    if (VComplexWidget::mouseDownEvent(x, y))
    {
        _pressed = true;

        if (_nopopup)
        {
            _status = !_status;
        }

        return true;
    }

    return false;
}

bool VButton::mouseUpEvent(int x, int y)
{
    if (VComplexWidget::mouseUpEvent(x, y))
    {
        if (_pressed &&
                (_clickevent != NULL))
        {
            _clickevent(x, y, this);
        }

        if (_pressed)
        {
            _pressed = false;
        }

        return true;
    }

    return false;
}

void VButton::setClickHandler(mouse_event_t handler)
{
    //if (handler != NULL)
    {
        _clickevent = handler;
    }
}

bool VButton::render() const
{
    if (!VRRWidget::render())
    {
        return false;
    }

    if (!_havepic)
    {
        return false;
    }

    if (_nopopup)
    {
        _pic1->setVisible(!_status);
        _pic2->setVisible(_status);
    }
    else
    {
        _pic1->setVisible(!_pressed);
        _pic2->setVisible(_pressed);
    }

    if (_pic1 == _pic2)
    {
        _pic1->setVisible(true);
    }

    glPushMatrix();
    _pic1->render();
    glPopMatrix();

    glPushMatrix();
    _pic2->render();
    glPopMatrix();

    return true;
}

void VButton::setNoPopup(bool no_popup)
{
    this->_nopopup = no_popup;
}

bool VButton::noPopup() const
{
    return this->_nopopup;
}

void VButton::setStatus(bool status)
{
    _status = status;
}

bool VButton::status() const
{
    return _status;
}

VPicture* VButton::currentPicture()
{
    if (_nopopup)
    {
        return _status ? _pic2 : _pic1;
    }

    return _pressed ? _pic2 : _pic1;
}

void VButton::move(int x, int y)
{
    VRRWidget::move(x, y);

    _pic1->move(x, y);
    _pic2->move(x, y);
}

void VButton::resize(int w, int h)
{
    VRRWidget::resize(w, h);

    _pic1->resize(w, h);
    _pic2->resize(w, h);
}

void VButton::setHavePicture(bool havepicture)
{
    if (_havepic = havepicture)
    {
        if (!_pic1)
        {
            _pic1 = new VPicture;
        }

        if (!_pic2)
        {
            _pic2 = new VPicture;
        }
    }
    else
    {
        if (_pic1 != _pic2)
        {
            SAFE_RELEASE(_pic1);
            SAFE_RELEASE(_pic2);
        }
        else
        {
            SAFE_RELEASE(_pic1);
        }
    }
}

bool VButton::havePicture()
{
    return _havepic;
}

VPicture* VButton::normalPicture()
{
    return _pic1;
}

VPicture* VButton::pressedPicture()
{
    return _pic2;
}

void VButton::init()
{

}
